<?php
namespace app\common\model;

use think\model\concern\SoftDelete;

class SystemMenu extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    /**
     * 获取菜单列表
     */
    public static function getMenuList($userId)
    {
        $userInfo = User::getFind(['id' => $userId]);
        $roleMenu = SystemRole::getVal(['id' => $userInfo['role_id']], 'rules');
        $menuIds  = explode(',', (string)$roleMenu);
        
        $where[] = ['pid', '=', 0];
        $where[] = ['is_show', '=', 1];
        $where[] = ['is_menu', '=', 1];
        $where[] = ['id', 'in', $menuIds];

        $list = self::where($where)->order('sort DESC')->select();

        $backData = [];
        foreach($list as $info){
            $backData[] = [
                'id'         => $info['id'],
                'is_red'     => MenuRed::getNum(['user_id' => $userId, 'menu_id' => $info['id'], 'is_read' => 0]),
                'path'       => '/' . $info['folder_name'],
                'component'  => 'Layout',
                'redirect'   => $info['redirect'],
                'alwaysShow' => true,
                'name'       => $info['name'],
                'meta'       => [
                    'title'  => $info['name'],
                    'icon'   => $info['icon'],
                ],
                'children'   => self::getMenuChildrenList($info['id'], $menuIds, $userId),
                'hidden'    => false,
            ];
        }

        return $backData;
    }

    /**
     * 获取子菜单
     */
    public static function getMenuChildrenList($pid = 0, $menuIds, $userId)
    {
        $where[] = ['pid', '=', $pid];
        $where[] = ['is_show', '=', 1];
        $where[] = ['is_menu', '=', 1];
        $where[] = ['id', 'in', $menuIds];

        $list = self::where($where)->order('sort DESC')->select();
        if(!$list) return [];

        $backData = [];
        foreach($list as $info){
            $backData[] = [
                'id'        => $info['id'],
                'is_red'    => MenuRed::getNum(['user_id' => $userId, 'menu_id' => $info['id'], 'is_read' => 0]),
                'path'      => $info['file_name'],
                'component' => $info['folder_name'] . '/' . $info['file_name'],
                'name'      => $info['name'],
                'meta'      => [
                    'title' => $info['name'],
                ],
                'hidden'    => false,
            ];
        }

        return $backData;
    }

    /**
     * 获取菜单红点列表
     */
    public static function getMenuRedList($userId)
    {
        $userInfo = User::getFind(['id' => $userId]);
        $roleMenu = SystemRole::getVal(['id' => $userInfo['role_id']], 'rules');
        $menuIds  = explode(',', (string)$roleMenu);
        
        $where[] = ['pid', '=', 0];
        $where[] = ['is_show', '=', 1];
        $where[] = ['is_menu', '=', 1];
        $where[] = ['id', 'in', $menuIds];

        $list = self::where($where)->order('sort DESC')->select();

        $backData = [];
        foreach($list as $info){
            $children = self::getMenuRedChildrenList($info['id'], $menuIds, $userId);
            $isRed = array_sum(array_column($children, 'is_red'));

            $backData[] = [
                'id'         => $info['id'],
                'is_red'     => $isRed,
                'children'   => $children,
            ];
        }

        return $backData;
    }

    /**
     * 获取子菜单
     */
    public static function getMenuRedChildrenList($pid = 0, $menuIds, $userId)
    {
        $where[] = ['pid', '=', $pid];
        $where[] = ['is_show', '=', 1];
        $where[] = ['is_menu', '=', 1];
        $where[] = ['id', 'in', $menuIds];

        $list = self::where($where)->order('sort DESC')->select();   
        if(!$list) return [];
        $list = $list->toArray();

        $backData = [];
        foreach($list as $info){
            $backData[] = [
                'id'        => $info['id'],
                'is_red'    => MenuRed::getNum([['user_id', '=', $userId], ['obj_user_id', '=', 0], ['menu_id', '=', $info['id']], ['is_read', '=', 0]]) 
                             + MenuRed::getNum([['user_id', '=', $userId], ['obj_user_id', '<>', 0], ['menu_id', '=', $info['id']], ['is_read', '=', 0]]),
            ];
        }

        return $backData;
    }

    /**
     * 设置红点
     */
    public static function setRed($userIds = [], $objUserId = 0, $menuId = 0, $objType = 0, $objId = 0)
    {
        $data  = [];
        foreach($userIds as $userId){
            $info = [
                'user_id'     => $userId,
                'obj_user_id' => $objUserId,
                'menu_id'     => $menuId,
                'obj_type'    => $objType,
                'obj_id'      => $objId,
            ];

            $data[] = $info;
        }

        MenuRed::addAll($data);
    }

    /**
     * 取消红点
     */
    public static function delRed($userId = 0, $menuId = 0, $objType = 0, $type = 1, $objId = 0)
    {
        if($type == 1){
            $where[] = ['user_id', '=', $userId];
        }else{
            $where[] = ['obj_user_id', '=', $userId];
        }

        $where[] = ['obj_id', '=', $objId];
        $where[] = ['menu_id', '=', $menuId];
        $where[] = ['obj_type', '=', $objType];

        MenuRed::editWhere(['is_read' => 1], $where);
    }
}